package com.gjy.service.uuid.controller;

import com.gjy.service.uuid.algorithm.SnowflakeIdGenerator;
import com.gjy.service.uuid.service.UuidGeneratorService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-10-12 10:22:31
 */
@RestController
@RequestMapping("/api/uuid")
public class UuidController {

    @Resource
    private UuidGeneratorService uuidGeneratorService;

    /**
     * 生成单个UUID
     *
     * @return 生成的UUID
     */
    @GetMapping("/generate")
    public ResponseEntity<Map<String, Object>> generateUuid() {
        try {
            String uuid = uuidGeneratorService.generateUuid();

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("uuid", uuid);
            response.put("timestamp", System.currentTimeMillis());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 生成指定数量的UUID
     *
     * @param count 生成数量，默认为1
     * @return 生成的UUID数组
     */
    @GetMapping("/generate/batch")
    public ResponseEntity<Map<String, Object>> generateUuids(
            @RequestParam(defaultValue = "1") int count) {
        try {
            if (count <= 0 || count > 1000) {
                Map<String, Object> response = new HashMap<>();
                response.put("success", false);
                response.put("error", "Count must be between 1 and 1000");
                return ResponseEntity.badRequest().body(response);
            }

            String[] uuids = uuidGeneratorService.generateUuids(count);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("uuids", uuids);
            response.put("count", uuids.length);
            response.put("timestamp", System.currentTimeMillis());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 生成基于时间的UUID
     *
     * @return 基于时间的UUID
     */
    @GetMapping("/generate/time-based")
    public ResponseEntity<Map<String, Object>> generateTimeBasedUuid() {
        try {
            String uuid = uuidGeneratorService.generateTimeBasedUuid();

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("uuid", uuid);
            response.put("type", "time-based");
            response.put("timestamp", System.currentTimeMillis());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 健康检查接口
     *
     * @return 服务状态
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("service", "UUID Service");
        response.put("timestamp", System.currentTimeMillis());

        return ResponseEntity.ok(response);
    }

    /**
     * 使用雪花算法生成唯一编码
     *
     * @param systemCode   系统编码
     * @param machineCode  机器编码
     * @param instanceCode 实例编码
     * @return 生成的唯一编码
     */
    @GetMapping("/generate/snowflake")
    public ResponseEntity<Map<String, Object>> generateSnowflakeId(
            @RequestParam String systemCode,
            @RequestParam String machineCode,
            @RequestParam String instanceCode) {
        try {
            String id = uuidGeneratorService.generateSnowflakeId(systemCode, machineCode, instanceCode);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("id", id);
            response.put("type", "snowflake");
            response.put("systemCode", systemCode);
            response.put("machineCode", machineCode);
            response.put("instanceCode", instanceCode);
            response.put("timestamp", System.currentTimeMillis());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 批量生成雪花算法ID
     *
     * @param systemCode   系统编码
     * @param machineCode  机器编码
     * @param instanceCode 实例编码
     * @param count        生成数量
     * @return 生成的ID数组
     */
    @GetMapping("/generate/snowflake/batch")
    public ResponseEntity<Map<String, Object>> generateSnowflakeIds(
            @RequestParam String systemCode,
            @RequestParam String machineCode,
            @RequestParam String instanceCode,
            @RequestParam(defaultValue = "10") int count) {
        try {
            if (count <= 0 || count > 1000) {
                Map<String, Object> response = new HashMap<>();
                response.put("success", false);
                response.put("error", "生成数量必须在1-1000之间");
                return ResponseEntity.badRequest().body(response);
            }

            String[] ids = uuidGeneratorService.generateSnowflakeIds(systemCode, machineCode, instanceCode, count);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("ids", ids);
            response.put("count", ids.length);
            response.put("type", "snowflake-batch");
            response.put("systemCode", systemCode);
            response.put("machineCode", machineCode);
            response.put("instanceCode", instanceCode);
            response.put("timestamp", System.currentTimeMillis());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 解析雪花算法ID
     *
     * @param id 雪花算法ID
     * @return ID组件信息
     */
    @GetMapping("/parse/snowflake")
    public ResponseEntity<Map<String, Object>> parseSnowflakeId(@RequestParam String id) {
        try {
            SnowflakeIdGenerator.IdComponents components = uuidGeneratorService.parseSnowflakeId(id);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("id", id);
            response.put("timestamp", components.getTimestamp());
            response.put("systemId", components.getSystemId());
            response.put("machineId", components.getMachineId());
            response.put("sequence", components.getSequence());
            response.put("parsedAt", System.currentTimeMillis());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(400).body(response);
        }
    }

    /**
     * 获取雪花算法生成器统计信息
     *
     * @return 统计信息
     */
    @GetMapping("/stats/snowflake")
    public ResponseEntity<Map<String, Object>> getSnowflakeStats() {
        try {
            Map<String, Object> stats = uuidGeneratorService.getSnowflakeGeneratorStats();
            stats.put("success", true);
            stats.put("timestamp", System.currentTimeMillis());

            return ResponseEntity.ok(stats);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());

            return ResponseEntity.status(500).body(response);
        }
    }

}

